package com.example.qzzm;

import com.amap.api.maps.AMap;
import com.amap.api.maps.AMap.OnMapLoadedListener;
import com.amap.api.maps.CameraUpdate;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.MapView;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.CameraPosition;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.MarkerOptions;
import com.example.qzzm.R;

import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.app.Activity;
import android.os.Bundle;

/**
 * 详情页面的Activity
 * 
 * @author Administrator
 *
 */
public class MapActivity extends Activity implements OnMapLoadedListener {
	// 返回按钮
	private ImageView ivBack;
	/* AMapV2地图中介绍如何使用mapview显示地图 */
	// 声明变量
	private MapView mapView;
	private AMap aMap;
	private LatLng latLng;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_map);
		// 在onCreat方法中给aMap对象赋值
		mapView = (MapView) findViewById(R.id.map);
		mapView.onCreate(savedInstanceState);// 必须要写
		aMap = mapView.getMap();
		aMap.setOnMapLoadedListener(this);

		initView();
		initListeners();

		// 传递坐标
		Bundle bundle = getIntent().getExtras();
		if (bundle != null && bundle.containsKey("locationX")
				&& bundle.containsKey("locationY")) {
			addMarker(bundle.getDouble("locationX"),
					bundle.getDouble("locationY"));
		}
	}

	/**
	 * 方法必须重写
	 */
	@Override
	protected void onResume() {
		super.onResume();
		mapView.onResume();
	}

	/**
	 * 方法必须重写
	 */
	@Override
	protected void onPause() {
		super.onPause();
		mapView.onPause();
	}

	/**
	 * 方法必须重写
	 */
	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		mapView.onSaveInstanceState(outState);
	}

	/**
	 * 方法必须重写
	 */
	@Override
	protected void onDestroy() {
		super.onDestroy();
		mapView.onDestroy();
	}

	// 初始化布局控件
	private void initView() {
		ivBack = (ImageView) findViewById(R.id.iv_back);

	}

	// 初始化监听事件
	private void initListeners() {
		ivBack.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});
	}

	// 增加坐标标记
	public void addMarker(double lat, double lon) {
		MarkerOptions markerOptions = new MarkerOptions();
		markerOptions.draggable(false);

		// 创建一个经纬度实例
		latLng = new LatLng(lat, lon);
		// 设置经纬度
		markerOptions.position(latLng);
		markerOptions.icon(BitmapDescriptorFactory
				.defaultMarker(BitmapDescriptorFactory.HUE_ROSE));
		markerOptions.perspective(true);
		aMap.addMarker(markerOptions);
	}

	@Override
	public void onMapLoaded() {
		// 设置相机的位置
		if (latLng == null) {
			// 创建一个默认的
			latLng = new LatLng(39.90403, 116.407525);// 北京经纬度
		}
		CameraUpdate cameraUpdate = CameraUpdateFactory
				.newCameraPosition(new CameraPosition(latLng, 15, 0, 30));
		aMap.animateCamera(cameraUpdate);
	}

}
